package com.CNMedical.web.controller.system.applet;

import com.alibaba.fastjson2.JSONObject;
import com.CNMedical.common.annotation.Anonymous;
import com.CNMedical.common.core.domain.R;
import com.CNMedical.common.utils.http.HttpRequest;
import com.CNMedical.system.domain.entity.AppletUser;
import com.CNMedical.system.service.IAppletUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录Controller
 *
 * @author tang
 * @date 2023-03-29
 */
@Api(tags = "登录相关接口")
@Anonymous
@RestController
@RequestMapping("/applet/login")
public class LoginController {

    @Value("${weChat.appId}")
    private String appId;

    @Value("${weChat.appSecret}")
    private String appSecret;

    @Autowired
    private IAppletUserService appletUserService;


    @ApiOperation("获取openId")
    @PostMapping("/getOpenId")
    public R getOpenId(@RequestBody JSONObject jsonObject1){


        String code = jsonObject1.getString("code");
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + appSecret + "&js_code=" + code+"&grant_type=authorization_code";
        String s = HttpRequest.sendGet(url);
        //返回的字符串转成   地址 json对象  返回
        JSONObject jsonObject = JSONObject.parseObject(s);
        return R.ok(jsonObject);
    }


    /**
     * 维修登陆注册
     * @param appletUser
     * @return
     */
    @ApiOperation("微信登陆注册")
    @PostMapping("/wxLogin")
    public R wxLogin(@RequestBody AppletUser appletUser){
        AppletUser u = appletUserService.wxLogin(appletUser);
        return R.ok(u);
    }
}
